#include <iostream>
using namespace std;

const int N = 1e6 + 10;

int l[N],r[N];

void dfsq(int u){
	cout << u << " ";
	if(l[u]) dfsq(l[u]);
	if(r[u]) dfsq(r[u]);
}

void dfsz(int u){
	if(l[u]) dfsz(l[u]);
	cout << u << " ";
	if(r[u]) dfsz(r[u]);
}

void dfsh(int u){
	if(l[u]) dfsh(l[u]);
	if(r[u]) dfsh(r[u]);
	cout << u << " ";
}


int main(){
	int n;
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> l[i] >> r[i];
	}
	dfsq(1);
    cout << endl;
	dfsz(1);
    cout << endl;
	dfsh(1);
    cout << endl;
	return 0;
}
